/**
 * 结束界面管理器
 */
cc.Class({
    extends: cc.Component,

    properties: {
        /**
         * 分数显示Label
         */
        scoreLabel: cc.Label,
        /**
         * 玩家节点
         */
        playerNode: cc.Node,
        /**
         * 子域显示图片
         */
        overDisplay: cc.Sprite,
    },

    onLoad () {
        this.playerNode.getComponent('Player').over = this;
        this.node.active = false;
        this._overScore = 0;
        this._isOver = false;
    },

    start () {
        this.rankTex = new cc.Texture2D();
    },

    update (dt) {
        this.updaetSubDomainCanvas();
    },

    /**
     * 更新子域的排行榜显示
     */
    updaetSubDomainCanvas () {
        if (!this.rankTex || !CC_WECHATGAME || !this._isOver) return;
        var openDataContext = wx.getOpenDataContext();
        var sharedCanvas = openDataContext.canvas;
        this.rankTex.initWithElement(sharedCanvas);
        this.rankTex.handleLoadedTexture();
        this.overDisplay.node.setContentSize(cc.director.getVisibleSize());
        this.overDisplay.spriteFrame = new cc.SpriteFrame(this.rankTex);
    },

    /**
     * 显示结束面板
     * @param {number} score 结算时的分数
     */
    showOverPanel (score) {
        this._overScore = score;
        this.node.active = true;
        this._isOver = true;
        //显示分数
        //this.showScore(this._overScore);
        this.uploadScore(this._overScore);
    },

    /**
     * 显示分数
     * @param {number} score 分数
     */
    showScore (score) {
        this.scoreLabel.string = score.toString();
    },

    /**
     * 点击分享（炫耀一下）按钮
     */
    onShareBtnClick () {
        if (!CC_WECHATGAME) return;
        wx.shareAppMessage({
            title: '我得了' + this._overScore.toString() + '分，快来挑战我！',
            imageUrl: cc.url.raw("resources/image/share.png")
        });
    },

    /**
     * 点击再来一次按钮
     */
    onRestartBtnClick () {
        cc.director.loadScene('Game');
        if (CC_WECHATGAME) {
            wx.postMessage({
                message: 'ChangeScene'
            });
        }
    },

    /**
     * 点击返回按钮
     */
    onBackBtnClick () {
        cc.director.loadScene('Start');
        if (CC_WECHATGAME) {
            wx.postMessage({
                message: 'ChangeScene'
            });
        }
    },

    /**
     * 将分数传到子域进行判断
     * @param {number} score 分数
     */
    uploadScore (score) {
        if (!CC_WECHATGAME) return;
        wx.postMessage({
            message: 'SetHighScore',
            score: score
        });
    },
});
